package com.itboye.mengwu.testpic.ui;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.ImageCompressAndUpload;
import com.itboye.core.utils.ImageCompressAndUpload.OnUploadlistener;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.ui.mymeng.MyGuanZhuActivity;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.luntan.comment.adapter.GridViewAdapter;
import com.itboye.mengwu.widget.PhotoSelectFragment;
import com.itboye.mengwu.widget.PhotoSelectFragment.PhotoSelectListener;

public class MePingJiaPublishedActivity extends BaseActivity implements
		Observer, OnClickListener {

	private GridView noScrollgridview;
	private GridViewAdapter adapter;
	private Button activity_selectimg_send;
	private ImageView imgPublishBack;
	// 标题
	private EditText editextTitle;
	// 内容
	private EditText editexContext;
	// private String title;
	private String contextEd;
	private String type;
	ImageView img;
	LinearLayout faceInputContainer;

	private ImageView miaosu1, miaosu2, miaosu3, miaosu4, miaosu5;
	private ImageView wuliu1, wuliu2, wuliu3, wuliu4, wuliu5;

	private ImageView taidu1, taidu2, taidu3, taidu4, taidu5;
	private boolean islike=true;
	
	private String uid;
	private String pid;
	private String content;
	private String score1=0+"";
	ArrayList<String> imgUrls = new ArrayList<>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub

		return R.layout.activity_item_shopcar;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		onLikeSelete();
		Init();
		imgPublishBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// showAlertDialog();

				finish();
			}
		});
	}

	/** 监听对话框里面的button点击事件 */
	DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
				finish();
				break;
			// case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
			// break;
			default:
				break;
			}
		}
	};
	private StringBuffer attachments;
	protected ArrayList<String> uploadedUrl = new ArrayList<String>();

	public void Init() {
		type = getIntent().getStringExtra("type");
		adapter = new GridViewAdapter(MePingJiaPublishedActivity.this, imgUrls);
		noScrollgridview.setAdapter(adapter);
		noScrollgridview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == imgUrls.size()) {
					new PopupWindows(MePingJiaPublishedActivity.this,
							noScrollgridview);
				} else {
					Intent intent = new Intent(MePingJiaPublishedActivity.this,
							PhotoActivity.class);
					intent.putExtra("imgUrls", imgUrls);
					intent.putExtra("ID", arg2);
					startActivity(intent);
				}
			}
		});
		activity_selectimg_send = (Button) findViewById(R.id.activity_selectimg_send);
		activity_selectimg_send.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showProgressDialog("正在发布中，请稍后", true);
				onBitMap();
			}

		});
	}

	public void onBitMap() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < imgUrls.size(); i++) {
			String Str = imgUrls.get(i).substring(
					imgUrls.get(i).lastIndexOf("/") + 1,
					imgUrls.get(i).lastIndexOf("."));
			list.add(FileUtils.SDPATH + Str + ".JPEG");
		}

		// 高清的压缩图片全部就在 list 路径里面了
		// 高清的压缩过的 bmp 对象 都在 Bimp.bmp里面
		// 完成上传服务器后 .........

		// title = editextTitle.getText().toString().trim();
		contextEd = editexContext.getText().toString().trim();

		// if (title == null || title == "") {
		// ByAlert.alert("请输入标题");
		// return;
		// }
		if (contextEd == null || contextEd == "") {
			ByAlert.alert("请输入内容");
			return;
		}
		final TieZiPresenter tanPresenter = new TieZiPresenter(this);
		if (imgUrls.size() > 0) {
			for (int j = 0; j < imgUrls.size(); j++) {
				System.out.println("路径" + imgUrls.get(j));
				ImageCompressAndUpload.getInstance().execute(imgUrls.get(j),
						new OnUploadlistener() {

							@Override
							public void OnSuccess(String resultData) {
								uploadedUrl.add(resultData);
								System.out.println("rse" + resultData);
								if (uploadedUrl.size() == imgUrls.size()) {
									if (uploadedUrl.size() > 0) {

										attachments = new StringBuffer();
										attachments.append(uploadedUrl.get(0));
										for (int i = 1; i < uploadedUrl.size(); i++) {
											attachments.append(",").append(
													uploadedUrl.get(i));
										}

									}
									WodeZengSongShangPingBean bean = new WodeZengSongShangPingBean();
									uid= SPUtils.get(MePingJiaPublishedActivity.this, null, SpPublicParams.UID, "")+ "";
									pid=bean.getPid();
									content = editexContext.getText().toString().trim();
									tanPresenter.ShopPingJia(uid, pid, content);
									ByAlert.alert("图片上传成功");
									try {
										closeProgressDialog();
									} catch (Exception e) {
										// TODO: handle exception
									}
								}

							}

							@Override
							public void OnError(String resultData) {
								// if (wait != null) {
								// wait.dismiss();
								// }
								//
								ByAlert.alert("发表出错了╰(￣▽￣)╭");
								try {
									closeProgressDialog();
								} catch (Exception e) {
									// TODO: handle exception
								}
								// toast.show();
							}
						},"bbs");
			}
		} else {
			WodeZengSongShangPingBean bean = new WodeZengSongShangPingBean();
			uid= SPUtils.get(MePingJiaPublishedActivity.this, null, SpPublicParams.UID, "")+ "";
			uid=bean.getPid();
			content = editexContext.getText().toString().trim();
			tanPresenter.ShopPingJia(uid, pid, content);
		}

		// bean.seta

		// FileUtils.deleteDir();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	protected void onRestart() {
		// adapter.update();
		super.onRestart();
	}

	public class PopupWindows extends PopupWindow {

		public PopupWindows(Context mContext, View parent) {

			View view = View
					.inflate(mContext, R.layout.item_popupwindows, null);
			view.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.fade_ins));
			LinearLayout ll_popup = (LinearLayout) view
					.findViewById(R.id.ll_popup);
			ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
					R.anim.push_bottom_in_2));

			setWidth(LayoutParams.FILL_PARENT);
			setHeight(LayoutParams.FILL_PARENT);
			setBackgroundDrawable(new BitmapDrawable());
			setFocusable(true);
			setOutsideTouchable(true);
			setContentView(view);
			showAtLocation(parent, Gravity.BOTTOM, 0, 0);
			update();

			Button bt1 = (Button) view
					.findViewById(R.id.item_popupwindows_camera);
			Button bt2 = (Button) view
					.findViewById(R.id.item_popupwindows_Photo);
			Button bt3 = (Button) view
					.findViewById(R.id.item_popupwindows_cancel);
			bt1.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					photo();
					dismiss();
				}
			});
			bt2.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// Intent intent = new Intent(PublishedActivity.this,
					// TestPicActivity.class);
					// startActivity(intent);
					// dismiss();
					// finish();
					new PhotoSelectFragment(new PhotoSelect(), 9).show(
							getSupportFragmentManager(), null);
				}
			});
			bt3.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dismiss();
				}
			});

		}
	}

	class PhotoSelect implements PhotoSelectListener {

		@Override
		public void onPhotoSelectFinish(List<String> paths) {
			for (String string : paths) {
				// XImageLoader.load("file://" + string,
				// (ImageView) v.findViewById(R.id.img));
				imgUrls.add(string);

			}
			adapter.notifyDataSetChanged();
		}

	}

	private static final int TAKE_PICTURE = 0x000000;
	private String path = "";

	public void photo() {
		Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory()
				+ "/myimage/", String.valueOf(System.currentTimeMillis())
				+ ".jpg");
		path = file.getPath();
		Uri imageUri = Uri.fromFile(file);
		openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		startActivityForResult(openCameraIntent, TAKE_PICTURE);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case TAKE_PICTURE:
			// if (Bimp.drr.size() < 9 && resultCode == -1) {
			// Bimp.drr.add(path);
			// }
			break;
		}
	}

	/**
	 * 退出提醒
	 */
	// public void showAlertDialog() {
	//
	// CustomDialog.Builder builder = new CustomDialog.Builder(this);
	// builder.setMessage("返回后将不会保存数据的哟！");
	// builder.setTitle("萌屋提示");
	// builder.setPositiveButton("确定要退出吗",
	// new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int which) {
	// dialog.dismiss();
	// // 设置你的操作事项
	// finish();
	//
	// }
	// });
	//
	// builder.setNegativeButton("取消",
	// new android.content.DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int which) {
	// dialog.dismiss();
	// }
	// });
	//
	// builder.create().show();
	//
	// }

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		ByAlert.alert("" + entity.getData());
		editextTitle.setText("");
		editexContext.setText("");
		Bimp.bmp.clear();
		adapter.notifyDataSetChanged();
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		setResult(102);
		finish();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.miaosu1:
			onIsLiKe();
			break;
		case R.id.miaosu2:
			miaosu1.setBackgroundResource(R.drawable.like_select);
			miaosu2.setBackgroundResource(R.drawable.like_select);
			miaosu3.setBackgroundResource(R.drawable.like_unselect);
			miaosu4.setBackgroundResource(R.drawable.like_unselect);
			miaosu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.miaosu3:
			miaosu1.setBackgroundResource(R.drawable.like_select);
			miaosu2.setBackgroundResource(R.drawable.like_select);
			miaosu3.setBackgroundResource(R.drawable.like_select);
			miaosu4.setBackgroundResource(R.drawable.like_unselect);
			miaosu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.miaosu4:
			miaosu1.setBackgroundResource(R.drawable.like_select);
			miaosu2.setBackgroundResource(R.drawable.like_select);
			miaosu3.setBackgroundResource(R.drawable.like_select);
			miaosu4.setBackgroundResource(R.drawable.like_select);
			miaosu5.setBackgroundResource(R.drawable.like_unselect);
			break;

		case R.id.miaosu5:
			miaosu1.setBackgroundResource(R.drawable.like_select);
			miaosu2.setBackgroundResource(R.drawable.like_select);
			miaosu3.setBackgroundResource(R.drawable.like_select);
			miaosu4.setBackgroundResource(R.drawable.like_select);
			miaosu5.setBackgroundResource(R.drawable.like_select);
			break;

		case R.id.wuliu1:
			wuliu1.setBackgroundResource(R.drawable.like_select);
			wuliu2.setBackgroundResource(R.drawable.like_unselect);
			wuliu3.setBackgroundResource(R.drawable.like_unselect);
			wuliu4.setBackgroundResource(R.drawable.like_unselect);
			wuliu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.wuliu2:
			wuliu1.setBackgroundResource(R.drawable.like_select);
			wuliu2.setBackgroundResource(R.drawable.like_select);
			wuliu3.setBackgroundResource(R.drawable.like_unselect);
			wuliu4.setBackgroundResource(R.drawable.like_unselect);
			wuliu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.wuliu3:
			wuliu1.setBackgroundResource(R.drawable.like_select);
			wuliu2.setBackgroundResource(R.drawable.like_select);
			wuliu3.setBackgroundResource(R.drawable.like_select);
			wuliu4.setBackgroundResource(R.drawable.like_unselect);
			wuliu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.wuliu4:
			wuliu1.setBackgroundResource(R.drawable.like_select);
			wuliu2.setBackgroundResource(R.drawable.like_select);
			wuliu3.setBackgroundResource(R.drawable.like_select);
			wuliu4.setBackgroundResource(R.drawable.like_select);
			wuliu5.setBackgroundResource(R.drawable.like_unselect);
			break;

		case R.id.wuliu5:
			wuliu1.setBackgroundResource(R.drawable.like_select);
			wuliu2.setBackgroundResource(R.drawable.like_select);
			wuliu3.setBackgroundResource(R.drawable.like_select);
			wuliu4.setBackgroundResource(R.drawable.like_select);
			wuliu5.setBackgroundResource(R.drawable.like_select);
			break;

		case R.id.taidu1:
			score1=1+"";
			taidu1.setBackgroundResource(R.drawable.like_select);
			taidu2.setBackgroundResource(R.drawable.like_unselect);
			taidu3.setBackgroundResource(R.drawable.like_unselect);
			taidu4.setBackgroundResource(R.drawable.like_unselect);
			taidu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.taidu2:
			taidu1.setBackgroundResource(R.drawable.like_select);
			taidu2.setBackgroundResource(R.drawable.like_select);
			taidu3.setBackgroundResource(R.drawable.like_unselect);
			taidu4.setBackgroundResource(R.drawable.like_unselect);
			taidu4.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.taidu3:
			taidu1.setBackgroundResource(R.drawable.like_select);
			taidu2.setBackgroundResource(R.drawable.like_select);
			taidu3.setBackgroundResource(R.drawable.like_select);
			taidu4.setBackgroundResource(R.drawable.like_unselect);
			taidu5.setBackgroundResource(R.drawable.like_unselect);
			break;
		case R.id.taidu4:
			taidu1.setBackgroundResource(R.drawable.like_select);
			taidu2.setBackgroundResource(R.drawable.like_select);
			taidu3.setBackgroundResource(R.drawable.like_select);
			taidu4.setBackgroundResource(R.drawable.like_select);
			taidu5.setBackgroundResource(R.drawable.like_unselect);
			break;

		case R.id.taidu5:
			taidu1.setBackgroundResource(R.drawable.like_select);
			taidu2.setBackgroundResource(R.drawable.like_select);
			taidu3.setBackgroundResource(R.drawable.like_select);
			taidu4.setBackgroundResource(R.drawable.like_select);
			taidu5.setBackgroundResource(R.drawable.like_select);
			break;
		default:
			break;
		}
	}

	public void onLikeSelete() {
		miaosu1.setBackgroundResource(R.drawable.like_unselect);
		miaosu2.setBackgroundResource(R.drawable.like_unselect);
		miaosu3.setBackgroundResource(R.drawable.like_unselect);
		miaosu4.setBackgroundResource(R.drawable.like_unselect);
		miaosu5.setBackgroundResource(R.drawable.like_unselect);

		wuliu1.setBackgroundResource(R.drawable.like_unselect);
		wuliu2.setBackgroundResource(R.drawable.like_unselect);
		wuliu3.setBackgroundResource(R.drawable.like_unselect);
		wuliu4.setBackgroundResource(R.drawable.like_unselect);
		wuliu5.setBackgroundResource(R.drawable.like_unselect);

		taidu1.setBackgroundResource(R.drawable.like_unselect);
		taidu2.setBackgroundResource(R.drawable.like_unselect);
		taidu3.setBackgroundResource(R.drawable.like_unselect);
		taidu4.setBackgroundResource(R.drawable.like_unselect);
		taidu5.setBackgroundResource(R.drawable.like_unselect);
	}
	
	public  void  onIsLiKe(){
		if(islike==false){
			miaosu1.setBackgroundResource(R.drawable.like_select);
			miaosu2.setBackgroundResource(R.drawable.like_unselect);
			miaosu3.setBackgroundResource(R.drawable.like_unselect);
			miaosu4.setBackgroundResource(R.drawable.like_unselect);
			miaosu5.setBackgroundResource(R.drawable.like_unselect);
		}else if(islike==true){
			miaosu1.setBackgroundResource(R.drawable.like_unselect);
			miaosu2.setBackgroundResource(R.drawable.like_unselect);
			miaosu3.setBackgroundResource(R.drawable.like_unselect);
			miaosu4.setBackgroundResource(R.drawable.like_unselect);
			miaosu5.setBackgroundResource(R.drawable.like_unselect);
		}
	}

}
